package scut.kent.shopping.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BaseServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1308260603506584689L;

	
	

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String method=request.getParameter("method");
		Method m=null;
		String jumpUrl;
		String red="redirect:";

		try {
			m=this.getClass().getMethod(method, HttpServletRequest.class,HttpServletResponse.class);
System.out.println(m.getName());
			jumpUrl=(String)m.invoke(this,request, response);
			
			if(jumpUrl.indexOf(red)!=-1){
				jumpUrl=jumpUrl.substring(red.length());
				response.sendRedirect(jumpUrl);
				return;
			}
			else{
				request.getRequestDispatcher("/WEB-INF/"+jumpUrl).forward(request, response);
			}
			
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
}
